package joe.works.pluginframework;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContextAware;

/**
 * Abstract implementation of {@link IPluginManager}. The Plugins are cached.
 * @author josseyj
 *
 */
public abstract class AbstractPluginManager implements ApplicationContextAware, IPluginManager {
	
	private Map<IExtensionPoint, List<IPlugin>> cache = new HashMap<IExtensionPoint, List<IPlugin>>();
	
	/**
	 * Loads the extension point with the plugins ({@link IPlugin}) for the extention point ({@link IExtensionPoint}) 
	 * @param extensionPoint
	 */
	public void loadExtensionPoint(IExtensionPoint extensionPoint) {
		List<IPlugin> plugins = getPlugins(extensionPoint);
		extensionPoint.loadPlugins(plugins);
	}
	
	protected List<IPlugin> getPlugins(IExtensionPoint extensionPoint) {
		List<IPlugin> plugins = cache.get(extensionPoint);
		if(plugins == null) {
			plugins = collectPlugins(extensionPoint);
			cache.put(extensionPoint, plugins);
		}
		return plugins;
	}
	
	/**
	 * Collects list of plugins ({@link IPlugin}) for the extension point ({@link IExtensionPoint}) 
	 * @param extensionPoint
	 * @return
	 */
	protected abstract List<IPlugin> collectPlugins(IExtensionPoint extensionPoint);
	
}
